package com.favccxx.search.api;

import com.favccxx.search.application.DataSourceService;
import com.favccxx.search.application.ProjectService;
import com.favccxx.search.domain.datasource.datasource.AbstractDataSource;
import com.favccxx.search.domain.datasource.datasource.JdbcConnection;
import com.favccxx.search.domain.datasource.datasource.JdbcDataSource;
import com.favccxx.search.domain.datasource.datasource.viewobject.JdbcDataSourceVo;
import com.favccxx.search.domain.project.Project;
import com.favccxx.search.infrastructure.util.rest.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author favccxx
 * @date 5/13/2020 3:14 PM
 */
@Slf4j
@Api(tags = "1.0 project Interface")
@RequestMapping("/project")
@RestController
public class ProjectController {


    @Autowired
    ProjectService projectService;

    @ApiOperation("1.1 Get Project list")
    @GetMapping("/list")
    public RestResult<List<Project>> findAll() {
        List<Project> list = projectService.findAll();
        return RestResult.sucess(list);
    }

    @ApiOperation("1.3 Insert project")
    @PutMapping("/insert")
    public RestResult<Project> insert(
            @RequestParam(value = "code", required = true) String code,
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "status", required = true, defaultValue = "ON") String status,
            @RequestParam(value = "description", required = false, defaultValue = "") String description,
            @RequestParam(value = "username", required = false) String username) {
        Project project = new Project();
        project.setCode(code);
        project.setName(name);
        project.setStatus(status);
        project.setDescription(description);
        projectService.insert(project);
        return RestResult.sucess(project);
    }

    @ApiOperation("1.4 update project")
    @PostMapping("/update")
    public RestResult<Project> update(
            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "status", required = true, defaultValue = "ON") String status,
            @RequestParam(value = "description", required = false, defaultValue = "") String description,
            @RequestParam(value = "username", required = false) String username) {
        Project project = new Project();
        project.setId(id);
        project.setName(name);
        project.setStatus(status);
        project.setDescription(description);
        projectService.update(project);
        return RestResult.sucess(project);
    }


}
